const app = getApp()
import { tsFormatTime } from '../../utils/util.js'
Page({
  data: {
    list: [],
    result: []
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '语会'
    })
    this.setData({ list: app.globalData.dialogList })
  },
  getTimeStr: (time) => {
    const currentDay = tsFormatTime(new Date(), 'Y-M-D')
    const myDay = tsFormatTime(time, 'Y-M-D')
    if (currentDay === myDay) {
      let hours = Number(tsFormatTime(time, 'h:m').split(':')[0])
      let munites = Number(tsFormatTime(time, 'h:m').split(':')[1])
      let str = ''
      if (hours > 12) {
        str = '下午'
        hours = hours - 12
      } else if (hours === 12) {
        str = '中午'
      } else {
        str = '上午'
      }
      return `${str} ${hours}:${munites}`
    }
    return tsFormatTime(time, 'M-D')
  },
  toMeeting: e => {
    fetch.post('/user/detailsByCode', { userCode: e.currentTarget.dataset.id }).then(data => {
      wx.hideLoading()
      const userInfo = {
        userId: data.userCode,
        avator: data.userPic,
        status: data.onlineStatus,
        name: data.userName
      }
      app.globalData.userInfo = userInfo
      var contactsCode;
      for (let i = 0; i < app.globalData.dialogList.length; i++) {
        if (e.currentTarget.dataset.id === app.globalData.dialogList[i].id) {
          contactsCode = this.data.list[i].contactsCode
          break;
        }
      }
      wx.navigateTo({
        url: '/pages/chat/index?contactsCode=' + contactsCode,
        fail: err => {
          console.log(err)
        }
      })
    }).catch(err => {
      console.log(err)
      wx.hideLoading()
      wx.showToast({
        title: '获取信息失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  search: function(e) {
    const list = this.data.list
    const value = e.detail.value
    if (value === '') {
      this.setData({ result: [] })
      return
    }
    const result = list.filter(x => x.name.indexOf(value) !== -1)
    this.setData({ result })
  },
  cancelSearch: function() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})
